#include "WeaponCapsule.h"

#define MOVING_SPEED 9.0f

CWeaponCapsule::CWeaponCapsule(void)
{
	_powerUp = new CPowerUp();
	_startFlag = false;
	_IsDead = false;
}


CWeaponCapsule::~CWeaponCapsule(void)
{
	delete _Sprite;
	delete _powerUp;
}

void CWeaponCapsule::Spawn(D3DXVECTOR2 position, int type)
{
	_defaultY = position.y;
	_x = position.x;
	_type = type;
}

void CWeaponCapsule::LoadResource(LPDIRECT3DDEVICE9 d3ddv, LPD3DXSPRITE &SpriteHandler, HINSTANCE hInstance, HWND hWnd)
{
	_Sprite = CSpriteManager::getInstance()->GetObjectSprite(Object::WeaponCapsule);
	_x = -100;
	_defaultY = -100;
	_alpha = 0.0f;
	_powerUp->LoadResource(d3ddv, SpriteHandler, hInstance, hWnd);
}

void CWeaponCapsule::Update(D3DXVECTOR2 ContraPosition)
{
	if(_startFlag)
	{
		if(!_IsDead)
		{
			_alpha += 0.1f;
			_x += MOVING_SPEED;
			_y = _defaultY + 30*sin(_alpha);
			_Sprite->Next();
			if(_x > ContraPosition.x + SCREEN_WIDTH/2)
			{
				_powerUp->Spawn(GetPosition(), _type);
				_IsDead = true;
				CExplosionManager::GetInstance()->Explode(_x, _y, 1);
			}
		}
	}
	else
	{
		if(ContraPosition.x > _x + SCREEN_WIDTH/2)
		{
			_startFlag = true;
		}
	}
}

void CWeaponCapsule::Render(D3DXVECTOR2 ContraPosition)
{
	Update(ContraPosition);
	if(_startFlag)
	{
		if(!_IsDead)
		{
			int vpx = ContraPosition.x - SCREEN_WIDTH/2;
			if (vpx<=0) 
				vpx=0;
			if (vpx >= GAMEWORLDWIDTH - SCREEN_WIDTH)
			{
				vpx = GAMEWORLDWIDTH - SCREEN_WIDTH;
			}
			_Sprite->Render(_x, _y);
		}
		else
		{
			_powerUp->Render(ContraPosition);
		}
	}
}